package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"savor_gin/controllers"
	. "savor_gin/errorType"
	"savor_gin/models"
	auth "savor_gin/utils/bcryptUtils"
	"savor_gin/utils/token"
	. "savor_gin/utils/token"
	"strconv"
	"time"
)

func GetUserApi(c *gin.Context) {
	if userList, err := models.GetUser(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		fmt.Println("正在查询用户列表")
		controllers.SendResp(c, OK, userList)
	}
}

func DeleteUserApi(c *gin.Context) {
	// 通过动态URL传递参数
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.UserInfo{UserId: uint64(id)}
	if err := s.Delete(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, nil)
}

func UpdateUserApi(c *gin.Context) {
	if contextData, exists := c.Get("contextData"); exists {
		if data, ok := contextData.(*TokenContext); ok {
			id := data.Id
			s := &models.UserInfo{UserId: uint64(id)}
			if err := c.ShouldBind(s); err != nil {
				controllers.SendResp(c, ParamBindError, err)
				return
			}
			if err := s.Update(); err != nil {
				controllers.SendResp(c, DataBaseError, err)
				return
			}
			controllers.SendResp(c, OK, s)
		}
	} else {
		controllers.SendResp(c, TokenTimeOverError, nil)
		return
	}
}

func GetUserById(c *gin.Context) {
	if contextData, exists := c.Get("contextData"); exists {
		if data, ok := contextData.(*TokenContext); ok {
			fmt.Println("User ID:", data.Id)
			fmt.Println("Username:", data.UserName)
			id := data.Id
			if s, err := models.GetUserInfoById(uint64(id)); err != nil {
				controllers.SendResp(c, DataBaseError, nil)
				return
			} else {
				fmt.Println("用户信息:", s)
				controllers.SendResp(c, OK, s)
			}
		}
	} else {
		// 处理找不到contextData的情况
		controllers.SendResp(c, TokenTimeOverError, nil)
		return
	}
}

func Register(c *gin.Context) {
	var user models.UserInfo
	if err := c.ShouldBindJSON(&user); err != nil {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	if user.IsRegistered(user.UserName) {
		controllers.SendResp(c, IsRegisterError, nil)
		return
	}
	// 加密密码
	if err := user.Encrypt(); err != nil {
		controllers.SendResp(c, AuthFailError, nil)
		return
	}
	if user.LastLoginDate.IsZero() {
		user.LastLoginDate = time.Now()
	}
	if err := user.Create(); err != nil {
		controllers.SendResp(c, DataBaseError, nil)
		return
	}
	controllers.SendResp(c, OK, user)
}

func Login(c *gin.Context) {
	var userlogin models.UserLogin
	if err := c.ShouldBindJSON(&userlogin); err != nil {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	u, err := models.GetUserByName(userlogin.UserName)
	if err != nil {
		controllers.SendResp(c, NoSuchUserError, nil)
		return
	}
	if err := auth.Compare(u.Password, userlogin.Password); err != nil {
		controllers.SendResp(c, PasswordAuthError, nil)
		return
	}
	// 生成token
	if t, err := token.Sign(token.TokenContext{Id: u.UserId, UserName: u.UserName}, ""); err != nil {
		controllers.SendResp(c, GreTokenError, nil)
		return
	} else {
		controllers.SendResp(c, OK, t)
	}

}
